
public class Buffer {
	
	private static Integer N = 3;
	private Integer quantos = 0;
	private Integer buffer[] = new Integer[N];
	private Integer proximaPosicaoInserir = 0;
	private Integer proximaPosicaoRemover = 0;
	public synchronized Integer removeElement() throws Exception {
		while (quantos == 0) {
			wait();
		}
		Integer retorno = buffer[proximaPosicaoRemover];	
		proximaPosicaoRemover = (proximaPosicaoRemover + 1)%N;
		quantos--;
		if (quantos == N-1) {
			notifyAll();
		}
		return retorno;
	}
	
	public synchronized void addElement(Integer element) throws Exception {
		while (quantos == N) {
			wait();
		}				
		buffer[proximaPosicaoInserir] = element;		
		proximaPosicaoInserir = (proximaPosicaoInserir + 1)%N; 
		quantos++;
		if (quantos == 1) {
			notifyAll();
		}		
	}


}
